home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / share / Dos / VARIOS / pascal / MOUSE.SWG / 0001_Mouse Positioning.pas
Encoding:
Pascal/Delphi Source File  |  1996-02-21  |  7.2 KB  |  197 lines

  1.  
  2. {Dear Gayle,
  3.  you make a very good job with SWAG. Let it grow...it's wonderful!
  4.  As a little contribution a small program for mouse-support is added.
  5.  If interested in more, I have a lot of graphic stuff in my bag.
  6.  
  7.  Happy christmas and a bug-free new 1996!
  8.  
  9.  Shortdescription:
  10.  Positioning under mouse-control needs calculation of relative mouse-movement.
  11.  Here's a small object (MouseSnap) that does this. The function SnapMove
  12.  returns True if mouse-position exceeds the snap value. the var parameter
  13.  holds the values for DeltaX and DeltaY. With a snap value of 1 normal
  14.  delta-movement is returned.
  15.  
  16.  Dec. 11, 1995, Udo Juerss, 57078 Siegen, Germany, CompuServe [101364,526]}
  17.  
  18. uses
  19.   Graph;
  20. {---------------------------------------------------------------------------}
  21.  
  22. type
  23.   Point           = record
  24.                       X : Integer;
  25.                       Y : Integer;
  26.                     end;
  27.  
  28.   TRect   = object                                           {Rechteckobjekt}
  29.     A : Point;                  {Startpunkt (oben links) X- und Y-Koordinate}
  30.     B : Point;                  {Endpunkt (unten rechts) X- und Y-Koordinate}
  31.     procedure Init(X1,Y1,X2,Y2:Integer);               {Koordinatenzuweisung}
  32.     procedure Move(Dx,Dy:Integer);                              {Verschieben}
  33.     procedure Draw(Color:Byte);
  34.   end;
  35.  
  36.   TMouseSnap = object
  37.     Snap        : Integer;                        {Fangwert f|r X/Y-Position}
  38.     DeltaX      : Integer;    {Differenz von aktueller zur letzen X-Position}
  39.     DeltaY      : Integer;    {Differenz von aktueller zur letzen Y-Position}
  40.     LastX       : Integer;              {Restbetrag von letzter X-Berechnung}
  41.     LastY       : Integer;              {Restbetrag von letzter Y-Berechnung}
  42.     Position    : Point;                              {Aktuelle Mausposition}
  43.     OldPosition : Point;                                {Letzte Mausposition}
  44.     procedure Init(ASnap:Integer);
  45.     function  SnapMove(var Delta:Point):Boolean;
  46.   end;
  47. {---------------------------------------------------------------------------}
  48.  
  49. var
  50.   Gd,Gm      : Integer;
  51.   P          : Point;
  52.   Rect       : TRect;
  53.   MouseSnap  : TMouseSnap;
  54.   IsSnap     : Boolean;
  55.   Snap       : Integer;
  56. {---------------------------------------------------------------------------}
  57.  
  58. procedure CursorOn; assembler;
  59. asm
  60.            mov   ax,1
  61.            int   33h
  62. end;
  63. {---------------------------------------------------------------------------}
  64.  
  65. procedure CursorOff; assembler;
  66. asm
  67.            mov   ax,2
  68.            int   33h
  69. end;
  70. {---------------------------------------------------------------------------}
  71.  
  72. function LeftButton:Boolean; assembler;
  73. asm
  74.            mov   ax,3
  75.            int   33h
  76.            mov   al,bl
  77.            and   al,1
  78. end;
  79. {---------------------------------------------------------------------------}
  80.  
  81. function RightButton:Boolean; assembler;
  82. asm
  83.            mov   ax,3
  84.            int   33h
  85.            mov   al,bl
  86.            and   al,2
  87. end;
  88. {---------------------------------------------------------------------------}
  89.  
  90. procedure GetMousePosition(var P:Point); assembler;
  91. asm
  92.            les   di,P
  93.            mov   ax,3
  94.            int   33h
  95.            mov   ax,cx
  96.            stosw
  97.            mov   ax,dx
  98.            stosw
  99. end;
  100. {---------------------------------------------------------------------------}
  101.  
  102. procedure TRect.Init(X1,Y1,X2,Y2:Integer);
  103. begin
  104.   A.X:=X1;
  105.   A.Y:=Y1;
  106.   B.X:=X2;
  107.   B.Y:=Y2;
  108. end;
  109. {---------------------------------------------------------------------------}
  110.  
  111. procedure TRect.Move(Dx,Dy:Integer);
  112. begin
  113.   A.X:=A.X + Dx;
  114.   A.Y:=A.Y + Dy;
  115.   B.X:=B.X + Dx;
  116.   B.Y:=B.Y + Dy;
  117. end;
  118. {---------------------------------------------------------------------------}
  119.  
  120. procedure TRect.Draw(Color:Byte);
  121. begin
  122.   SetColor(Color);
  123.   CursorOff;
  124.   Line(A.X,A.Y,B.X,A.Y);
  125.   Line(A.X,B.Y,B.X,B.Y);
  126.   Line(A.X,A.Y,A.X,B.Y);
  127.   Line(B.X,A.Y,B.X,B.Y);
  128.   CursorOn;
  129. end;
  130. {---------------------------------------------------------------------------}
  131.  
  132. procedure TMouseSnap.Init(ASnap:Integer);        {Initialisierung TMouseSnap}
  133. begin
  134.   Snap:=ASnap;                      {Parameter f|r X/Y-Fangraster |bernehmen}
  135.   if Snap <= 0 then Snap:=1;                 {Division durch Null verhindern}
  136.   DeltaX:=0;                                        {Variable initialisieren}
  137.   DeltaY:=0;                                        {Variable initialisieren}
  138.   LastX:=0;                                         {Variable initialisieren}
  139.   LastY:=0;                                         {Variable initialisieren}
  140.   GetMousePosition(Position); {Mausposition lesen, damit Position definieren}
  141.   OldPosition:=Position;                             {OldPosition definieren}
  142. end;
  143. {---------------------------------------------------------------------------}
  144.  
  145. function TMouseSnap.SnapMove(var Delta:Point):Boolean;
  146. begin
  147.   SnapMove:=False;                           {Funtionsergebnis voreinstellen}
  148.   GetMousePosition(Position);                   {Aktuelle Mausposition holen}
  149.   if (Position.X <> OldPosition.X) or (Position.Y <> OldPosition.Y) then
  150.   begin                {Wenn aktuelle Position ungleich der vorherigen, dann}
  151.     DeltaX:=Position.X - OldPosition.X;      {Delta der X-Position berechnen}
  152.     DeltaY:=Position.Y - OldPosition.Y;      {Delta der Y-Position berechnen}
  153.     Inc(DeltaX,LastX);{Delta um den Restbetrag der letzen Berechnung erhvhen}
  154.     Inc(DeltaY,LastY);{Delta um den Restbetrag der letzen Berechnung erhvhen}
  155.     LastX:=DeltaX mod Snap;               {Neuen Restbetrag in LastX sichern}
  156.     LastY:=DeltaY mod Snap;               {Neuen Restbetrag in LastY sichern}
  157.     DeltaX:=DeltaX div Snap;                   {DeltaX durch Fangwert teilen}
  158.     DeltaY:=DeltaY div Snap;                   {DeltaY durch Fangwert teilen}
  159.     DeltaX:=DeltaX * Snap;     {DeltaX = 0/Fangwert/Vielfaches des Fangwerts}
  160.     DeltaY:=DeltaY * Snap;     {DeltaY = 0/Fangwert/Vielfaches des Fangwerts}
  161.     if (Word(DeltaX) >= Snap) or (Word(DeltaY) >= Snap) then
  162.     begin {Wenn DeltaX oder DeltaY gleich oder grv_er als Fangwert ist, dann}
  163.       Delta.X:=DeltaX;                      {R|ckgabewert X von Punkt setzen}
  164.       Delta.Y:=DeltaY;                      {R|ckgabewert Y von Punkt setzen}
  165.       SnapMove:=True;                              {Funktionsergebnis setzen}
  166.     end;
  167.   end;
  168.   OldPosition:=Position;   {Aktuelle Position f|r ndchsten Vergleich sichern}
  169. end;
  170. {---------------------------------------------------------------------------}
  171.  
  172. begin
  173.   Gd:=Detect;
  174.   InitGraph(Gd,Gm,'c:\tp\system');           {Change to your BGI driver path}
  175.   CursorOn;
  176.   Rect.Init(0,0,50,50);
  177.   IsSnap:=True;
  178.   Snap:=25;                                {Change this value for other snap}
  179.   MouseSnap.Init(Snap);
  180.   Rect.Draw(White);
  181.   repeat
  182.     if LeftButton then                      {Press LeftButton to toggle snap}
  183.     begin
  184.       repeat until not LeftButton;
  185.       IsSnap:=IsSnap xor True;
  186.       if IsSnap then MouseSnap.Init(Snap) else MouseSnap.Init(1);
  187.     end;
  188.     if MouseSnap.SnapMove(P) then
  189.     begin
  190.       Rect.Draw(Black);
  191.       Rect.Move(P.X,P.Y);
  192.       Rect.Draw(White);
  193.     end;
  194.   until RightButton;                      {Press RightButton to quit program}
  195.   CloseGraph;
  196. end.
  197.